var game
//当前游戏地图
var maps
var map;
var index=0;

//游戏地图的宽和高
var  height
var width
//游戏地图的宽和高
var imgheight
var  imgwidth
//定义人物坐标
var peoplePoint=new Object()
//下一位置的坐标
var nextPoint=new Object()
//下下一个位置的坐标
var  nNextPoint=new  Object()

var  destation=new Array()


//开始游戏
function test(){
	 loadmap()
	creatMap()
}
//键盘命令

function keydown(){
	var code=window.event.keyCode
	
	switch (code){
		case 37://左
		     nextPoint.x=peoplePoint.x-1
		     nextPoint.y=peoplePoint.y
		     
	         nNextPoint.x=peoplePoint.x-2
		     nNextPoint.y=peoplePoint.y
			break;
		case 38://上
		     nextPoint.x=peoplePoint.x
		     nextPoint.y=peoplePoint.y-1
		     
		     
		     nNextPoint.x=peoplePoint.x
		     nNextPoint.y=peoplePoint.y-2
			break;
		case 39://右
		     nextPoint.x=peoplePoint.x+1
		     nextPoint.y=peoplePoint.y
		     
		     nNextPoint.x=peoplePoint.x+2
		     nNextPoint.y=peoplePoint.y
			break;
		case 40://下
		     nextPoint.x=peoplePoint.x
		     nextPoint.y=peoplePoint.y+1
		     
		     nNextPoint.x=peoplePoint.x
		     nNextPoint.y=peoplePoint.y+2
			break;	
	}
	peopleMove()
}
//人物开始移动
function  peopleMove(){
	
	var  pointvalue=map[nextPoint.y][nextPoint.x]
	var  peopleId=document.getElementById(peoplePoint.x+''+peoplePoint.y)
	var imgId=document.getElementById(nextPoint.x+''+nextPoint.y)
//	alert(peoplePoint.x+' '+peoplePoint.y)
//	alert(nextPoint.x+' '+nextPoint.y)
	if(pointvalue==2||pointvalue==3){
		//图片转换
	
		peopleId.src="img/2.png"
		imgId.src="img/6.png"
		//地图genghuan
		 map[peoplePoint.y][peoplePoint.x]=2
		 map[nextPoint.y][nextPoint.x]=6
		 
		 peoplePoint.x=nextPoint.x
		 peoplePoint.y=nextPoint.y
	}else if(pointvalue==4){
		   var nextPointValue=map[nNextPoint.y][nNextPoint.x]
		   if(nextPointValue==2||nextPointValue==3){
		   	var nimg=document.getElementById(nNextPoint.x+''+nNextPoint.y);
			peopleId.src="img/2.png";
			imgId.src="img/6.png";
			nimg.src="img/4.png";
			map[peoplePoint.y][peoplePoint.x]=2;
			map[nextPoint.y][nextPoint.x]=6;
			map[nNextPoint.y][nNextPoint.x]=4;
			
			peoplePoint.x=nextPoint.x
			peoplePoint.y=nextPoint.y
		   }
	}
	mapChange()
}
//地图转换

function    mapChange(){
	var count=0;
	for(var i=0;i<destation.length;i++){
		if(!(map[destation[i].y][destation[i].x]==6||map[destation[i].y][destation[i].x]==4))
{
			
			map[destation[i].y][destation[i].x]=3
			var img=document.getElementById(destation[i].x+''+destation[i].y)
			
			img.src="img/3.png"
		}
		if(map[destation[i].y][destation[i].x]==4){
			count++;
			if(count==destation.length){
				alert('恭喜,过关!');
				index++;
				creatMap()
			}
		}
	}
}
//判断箱子推不动时结束



//创建地图
function  creatMap(){
	map=maps[index]
	var msg=""
	for(var i=0;i<map.length;i++){
		for(var j=0;j<map[i].length;j++){
			msg+="<img src='img/"+map[i][j]+".png' id='"+j+i+"' height='"+imgheight+"px' width='"+imgwidth+"px'/>"
			if(map[i][j]==6){
				peoplePoint.x=j
				peoplePoint.y=i
			}
		}
	}
	game.innerHTML=msg
}
//初始化信息
function loadmap(){
	//初始化数组
	 maps=[
	       [
			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
			[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 1, 0, 0],
			[0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 2, 6, 1, 0, 0],
			[0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 3, 2, 2, 1, 0, 0],
			[0, 0, 0, 0, 0, 0, 1, 4, 2, 4, 2, 4, 2, 1, 0, 0],
			[0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0]
		]
	];
	
	//获取地图div 
   game=document.getElementById("game")
   //初始化地图大小
   height=parseInt(game.style.height)
   width=parseInt(game.style.width)
   
   //初始化格字大小
   
   imgheight=height/maps[0].length
   imgwidth=width/maps[0][0].length
}
